package com.hottv.meeting.videomeeting.module.meeting;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;

import com.hottv.meeting.videomeeting.common.MeetingConfig;
import com.hottv.meeting.videomeeting.module.edp.EDPUtils;
import com.hotwater.uacclient.IInfoAccess;

/**
 * Created by chengwenqiang on 2019/6/6.
 */
//
public class InfoAccessImpl extends IInfoAccess.Stub {

    private static final String TAG = "InfoAccessImpl";

    public static final String DEVICEID = "DeviceID";

    public static final String ONENET_ID = "One_net_ID";

    public static final String TOKEN_ID = "TOKEN_ID";

    public static final String HOTTV_ID = "HOTTV_ID";

    private static InfoAccessImpl instance;

    private Context context;

    private Boolean mIsBind =false;

    private com.hotwater.uacclient.IInfoAccess mDataService;

    public InfoAccessImpl(Context context) {
        this.context = context;

        bindAuthService();
    }

    public static InfoAccessImpl getInstance(Context context) {
        if (null == instance) {
            synchronized (InfoAccessImpl.class) {
                if (null == instance) {
                    instance = new InfoAccessImpl(context);
                }
            }
        }
        return instance;
    }

    private void bindAuthService(){
        Intent intent = new Intent("com.hotwater.uacclient.IInfoAccess");
        intent.setPackage("com.hotwater.uacclient");
        mIsBind = this.context.bindService(intent,conn, Context.BIND_AUTO_CREATE);
    }

    private void  getOneNetId()
    {
        String deviceId ="";
        try{
            deviceId =  getInfoData(ONENET_ID,"");
        }catch (RemoteException e)
        {
            e.printStackTrace();
        }

        Log.i(MeetingConfig.TAG,"getOneNetId:"+deviceId);
        EDPUtils.edp_devices_Id = deviceId;
    }

    public void unbindAuthService()
    {
        if(mIsBind) {
            mIsBind =false;
            this.context.unbindService(conn);
        }
    }

    private ServiceConnection conn = new ServiceConnection() {
        /** 获取服务对象时的操作 */
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            Log.i(MeetingConfig.TAG,"onServiceConnected");
            mDataService = IInfoAccess.Stub.asInterface(service);
            getOneNetId();
        }

        /** 无法获取到服务对象时的操作 */
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            Log.i(MeetingConfig.TAG,"onServiceDisconnected");
            mDataService = null;
        }
    };

    @Override
    public String getInfoData(String dataName, String extData) throws RemoteException {

        Log.i(TAG,"getSTBData"+dataName + " extData " + extData);
        if(mDataService!=null)
        {
            String value =  mDataService.getInfoData(dataName,extData);
            Log.i(TAG,"getSTBData:"+dataName+",value:"+value);
            return value;
        }else {
            bindAuthService(); //绑定服务
        }
        return "";
    }
}
